<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>与Generator函数结合</title>
</head>

<body>
    <script>
        const fna = () => {
            return Promise.resolve('fna...');
        };
        const fnb = () => {
            return Promise.resolve('fnb...');
        }

        function* generator() {
            try {
                const fnas = yield fna();
                const fnbs = yield fnb();
                console.log(fnas, fnbs);
            } catch (err) {
                console.log(err);
            }
        }

        function run(generator) {
            const g = generator();

            function go(result) {
                if (result.done) return result.value;
                result.value.then(
                    val => {
                        return go(g.next(val));
                    },
                    err => {
                        return go(g.throw(err));
                    });
            }

            go(g.next());
        }

        run(generator);
    </script>
</body>

</html>